﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Answer : Control
    {
        public event EventHandler Marked;
        public event EventHandler Unmarked;

        private Canvas _answer, _button;
        private Ellipse _circle;
        private TextBlock _text;

        private bool _isCorrect, _isMarked;

        /// <summary>
        /// //
        /// </summary>

        /// <param name="text">
        /// //
        /// </param>

        /// <param name="isCorrect">
        /// //
        /// </param>

        public Answer(string text, bool isCorrect)
            : base()
        {
            FrameworkElement resource = InitializeFromXaml(new StreamReader(GetType().Assembly.GetManifestResourceStream("AgLux.Component.Task.Answer.xaml")).ReadToEnd());

            _answer = (Canvas)resource.FindName("answer");
            _button = (Canvas)resource.FindName("button");
            _circle = (Ellipse)resource.FindName("circle");
            _text = (TextBlock)resource.FindName("text");

            _isCorrect = isCorrect;
            _isMarked = false;

            _answer.MouseLeftButtonDown += new MouseEventHandler(AnswerPressed);

            _text.Text = text;
            _text.SetValue(Canvas.LeftProperty, 20);
        }

        /// <summary>
        /// //
        /// </summary>

        /// <returns>
        /// //
        /// </returns>

        public bool IsMarked
        {
            get
            {
                return _isMarked;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="canvasWidth">
        /// //
        /// </param>

        public void FitCanvas(double canvasWidth)
        {
            Width = _answer.Width = canvasWidth;
            _text.Width = Width - 20;

            Height = _answer.Height = _text.Height = _text.ActualHeight;
            _button.SetValue(Canvas.TopProperty, Height / 2 - 5);
        }

        /// <summary>
        /// //
        /// </summary>

        public void Mark()
        {
            _circle.Fill = new SolidColorBrush(Colors.Gray);
            _isMarked = true;
        }

        /// <summary>
        /// //
        /// </summary>

        public void Unmark()
        {
            _circle.Fill = new SolidColorBrush(Colors.Black);
            _isMarked = false;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <returns>
        /// //
        /// </returns>

        public bool ShowCorrectness()
        {
            if (_isCorrect)
            {
                _circle.Fill = new SolidColorBrush(Colors.Green);
                _text.Foreground = new SolidColorBrush(Colors.Green);
                _text.Text += "   CORRECT";
            }

            else
            {
                _circle.Fill = new SolidColorBrush(Colors.Red);
                _text.Foreground = new SolidColorBrush(Colors.Red);
                _text.Text += "   INCORRECT";
            }

            return _isCorrect;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        private void AnswerPressed(object sender, EventArgs args)
        {
            if (_isMarked)
            {
                OnUnmarked(new EventArgs());
            }

            else
            {
                OnMarked(new EventArgs());
            }
        }
        
        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>

        private void OnMarked(EventArgs args)
        {
            if (Marked != null)
            {
                Marked(this, args);
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>
        
        private void OnUnmarked(EventArgs args)
        {
            if (Unmarked != null)
            {
                Unmarked(this, args);
            }
        }
    }
}
